// 预测占用情况图表模块
const PredictionOccupancyChart = {
  chartInstance: null,
  
  // 初始化预测占用情况图表
  init: function(containerId) {
    const container = document.getElementById(containerId);
    if (!container) {
      console.error(`预测占用情况图表容器 ${containerId} 未找到`);
      return null;
    }

    // 检查并设置最小容器尺寸
    if (container.offsetWidth < 300) {
      container.style.minWidth = '100%';
    }
    if (container.offsetHeight < 350) {
      container.style.minHeight = '380px';
    }

    this.chartInstance = echarts.init(container);
    
    // 强制重新计算尺寸
    setTimeout(() => {
      if (this.chartInstance) {
        this.chartInstance.resize();
      }
    }, 100);
    
    // 注册到联动管理器
    ChartSync.registerChart(this.chartInstance);
    
    console.log('[INFO] 预测占用情况图表初始化完成');
    return this;  // 返回模块对象本身，而不是ECharts实例
  },

  // 更新预测占用情况图表数据
  update: function(timeData, occupancyData) {
    if (!this.chartInstance) {
      console.error('[ERROR] 预测占用情况图表未初始化');
      return;
    }

    const option = {
      ...ChartConfig.commonChartConfig,
      title: {
        text: '预测房间占用情况与综合分析',
        left: 'center'
      },
      tooltip: {
        ...ChartConfig.commonTooltipConfig,
        trigger: 'axis',
        axisPointer: {
          type: 'cross',
          animation: false,
          lineStyle: {
            color: 'rgba(30, 144, 255, 0.8)',
            width: 1.5
          },
          crossStyle: {
            color: 'rgba(30, 144, 255, 0.8)',
            width: 1.5
          }
        }
      },
      grid: ChartConfig.getGridConfig('default'),
      xAxis: ChartConfig.getXAxisConfig(timeData, 12),
      yAxis: [
        {
          type: 'value',
          name: '人数',
          position: 'left',
          axisLabel: {
            formatter: '{value} 人'
          },
          splitLine: {
            show: false
          }
        },
        {
          type: 'value',
          name: 'CO2/PIR',
          position: 'right',
          splitLine: {
            show: false
          }
        }
      ],
      series: [
        {
          name: '预测房间人数',
          type: 'line',
          yAxisIndex: 0,
          data: occupancyData.occupancy,
          symbol: 'circle',
          symbolSize: 4,
          lineStyle: {
            width: 2,
            color: '#ff6b6b',
            type: 'dashed'  // 预测数据使用虚线
          },
          itemStyle: {
            color: '#ff6b6b'
          },
          emphasis: {
            focus: 'series'
          },
          sampling: 'average',
          large: true
        },
        {
          name: '预测CO2浓度',
          type: 'line',
          yAxisIndex: 1,
          data: occupancyData.co2,
          symbol: 'none',
          lineStyle: {
            width: 1.5,
            color: '#4ecdc4',
            type: 'dashed'  // 预测数据使用虚线
          },
          emphasis: {
            focus: 'none'
          },
          sampling: 'average',
          large: true
        },
        {
          name: '预测PIR运动检测',
          type: 'line',
          yAxisIndex: 1,
          data: occupancyData.pir,
          symbol: 'none',
          lineStyle: {
            width: 1.5,
            color: '#45b7d1',
            type: 'dashed'  // 预测数据使用虚线
          },
          emphasis: {
            focus: 'none'
          },
          sampling: 'average',
          large: true
        },
        {
          name: '预测置信度',
          type: 'line',
          yAxisIndex: 1,
          data: occupancyData.confidence,
          symbol: 'none',
          lineStyle: {
            width: 1.5,
            color: '#ffa726',
            type: 'dashed'  // 预测数据使用虚线
          },
          emphasis: {
            focus: 'none'
          },
          sampling: 'average',
          large: true
        }
      ],
      dataZoom: ChartConfig.getDataZoomConfig(40)
    };

    this.chartInstance.setOption(option, {
      notMerge: true,
      lazyUpdate: true,
      silent: true
    });
  },

  // 获取图表实例
  getInstance: function() {
    return this.chartInstance;
  },

  // 调整图表大小
  resize: function() {
    if (this.chartInstance) {
      this.chartInstance.resize();
    }
  },

  // 清空图表
  clear: function() {
    if (this.chartInstance) {
      this.chartInstance.clear();
    }
  },

  // 销毁图表
  dispose: function() {
    if (this.chartInstance) {
      ChartSync.unregisterChart(this.chartInstance);
      this.chartInstance.dispose();
      this.chartInstance = null;
    }
  }
};

// 导出到全局
window.PredictionOccupancyChart = PredictionOccupancyChart; 